﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Admin;

namespace Common.Controls
{
    public partial class ValidateClaims : UserControl
    {
        StudyManager _studyManager = StudyManager.GetInstance();
        StudyDataClient _studyDataClient = new StudyDataClient();
        Guid _studyId;
        List<Claim> _claimsForStudy;
        List<IndividualClaimsView> _claimsForIndividual;
        string _loginName;
        int _individualId;
        public ValidateClaims()
        {
            InitializeComponent();
        }
        public ValidateClaims(Guid StudyId, string LoginName, int IndividualId)
        {
            InitializeComponent();
            _studyId = StudyId;
            _loginName = LoginName;
            _individualId = IndividualId;
            BindData();
        }
        private void BindData()
        {
            _studyDataClient.GetClaimsForStudyEvent += new StudyDataClient.GetClaimsForStudyDelegate(delegate(List<Claim> Claims)
            {
                _claimsForStudy = Claims;
                _studyDataClient.GetClaimsForIndividual(_loginName, (from x in _claimsForStudy
                                                                  select x.ClaimId).ToList());
            });

            _studyDataClient.GetClaimsForIndividualEvent += new StudyDataClient.GetClaimsForIndividualDelegate(delegate(List<IndividualClaimsView> IndividualClaims)
            {
                _claimsForIndividual = IndividualClaims;                
                PopulateControls();
            });
            _studyDataClient.GetClaimsForStudy(_studyId);            
        }
        private void PopulateControls()
        {
            //Hide the claims not needed
            List<int> ClaimIds = (from x in _claimsForStudy
                                  select x.ClaimId).ToList();            
            foreach (UIElement element in LayoutRoot.Children)
            {
                if (element is StackPanel)
                {
                    StackPanel myPanel = element as StackPanel;
                    if (myPanel.Tag != null)
                    {
                        int ClaimId = Int32.Parse(myPanel.Tag.ToString());
                        if (ClaimIds.Contains(ClaimId) == false)
                        {
                            myPanel.Visibility = Visibility.Collapsed;
                        }
                    }
                }
                else if (element is Grid)
                {
                    Grid myPanel = element as Grid;
                    if (myPanel.Tag != null)
                    {
                        int ClaimId = Int32.Parse(myPanel.Tag.ToString());
                        if (ClaimIds.Contains(ClaimId) == false)
                        {
                            myPanel.Visibility = Visibility.Collapsed;
                        }
                    }
                }
            }
            // Add values to all the controls
            foreach (IndividualClaimsView iv in _claimsForIndividual)
            {
                switch (iv.claimIdField)
                {
                    case 1: //Gender
                        break;
                    case 2: //Birthdate
                        calBirthDate.SelectedDate = DateTime.Parse(iv.valueField);
                        break;
                    case 3: //Address
                        tbAddress.Text = iv.valueField;
                        break;
                    case 4: //City
                        tbCity.Text = iv.valueField;
                        break;
                    case 5: //State
                        tbState.Text = iv.valueField;
                        break;
                    case 6: //ZipCode
                        tbZipCode.Text = iv.valueField;
                        break;
                    case (int)StudyManager.ClaimEnum.Country: //Country
                        tbCountry.Text = iv.valueField;
                        break;
                    case (int)StudyManager.ClaimEnum.GivenName: //Given Name
                        tbFirstName.Text = iv.valueField;
                        break;
                    case 10: //Surname
                        tbLastName.Text = iv.valueField;
                        break;
                    case 11: //EmailAddress Address
                        tbEmailAddress.Text = iv.valueField;
                        break;
                    case 12: //Study ID
                        tbStudentId.Text = iv.valueField;
                        break;

                }
            }
            
        }
        private void PerformValidation()
        {
            btnSubmit.IsEnabled = ValidatePage();
        }
        private bool ValidatePage()
        {
            bool Validated = true;
            if (pnlEmailAddress.Visibility == Visibility.Visible
                && tbEmailAddress.Validated == false)
                return false;
            if (pnlStudentId.Visibility == Visibility.Visible
                && string.IsNullOrEmpty(tbStudentId.Text) == true)
                return false;
            if (pnlFirstName.Visibility == Visibility.Visible
                && string.IsNullOrEmpty(tbFirstName.Text) == true)
                return false;
            if (pnlLastName.Visibility == Visibility.Visible
                && string.IsNullOrEmpty(tbLastName.Text))
                return false;
            if (pnlGender.Visibility == Visibility.Visible
                && ddlGender.SelectedIndex == 0)
                return false;
            if (pnlBirthDate.Visibility == Visibility.Visible
                && calBirthDate.SelectedDate.HasValue == false)
                return false;
            if (pnlAddress.Visibility == Visibility.Visible
                && string.IsNullOrEmpty(tbAddress.Text) == true)
                return false;
            if (pnlCity.Visibility == Visibility.Visible
                && string.IsNullOrEmpty(tbCity.Text) == true)
                return false;
            if (pnlState.Visibility == Visibility.Visible
                && string.IsNullOrEmpty(tbState.Text) == true)
                return false;
            if (pnlZipCode.Visibility == Visibility.Visible
                && string.IsNullOrEmpty(tbZipCode.Text) == true)
                return false;
            
            return Validated;
        }
        private void SaveAllValues()
        {   
            if (pnlEmailAddress.Visibility == Visibility.Visible)
            {
                 _studyDataClient.CreateClaim(_loginName, 11, tbEmailAddress.Text);
            }
            if (pnlStudentId.Visibility == Visibility.Visible)
            {
                _studyDataClient.CreateClaim(_loginName, 12, this.tbStudentId.Text);
            }
            if (pnlFirstName.Visibility == Visibility.Visible)
            {
                _studyDataClient.CreateClaim(_loginName, 8, tbFirstName.Text);
            }
            if (pnlLastName.Visibility == Visibility.Visible)
            {
                _studyDataClient.CreateClaim(_loginName, 10, tbLastName.Text);
            }
            if (pnlGender.Visibility == Visibility.Visible)
            {
                _studyDataClient.CreateClaim(_loginName, 1, ddlGender.SelectedItem.ToString());
            }
            if (pnlBirthDate.Visibility == Visibility.Visible)
            {
                _studyDataClient.CreateClaim(_loginName, 2, calBirthDate.SelectedDate.ToString());
            }
            if (pnlAddress.Visibility == Visibility.Visible)
            {
                _studyDataClient.CreateClaim(_loginName, 3, tbAddress.Text);
            }
            if (pnlCity.Visibility == Visibility.Visible)
            {
                _studyDataClient.CreateClaim(_loginName, 4, tbCity.Text);
            }
            if (pnlState.Visibility == Visibility.Visible) {
                _studyDataClient.CreateClaim(_loginName, 5, tbState.Text);
            }
            if (pnlZipCode.Visibility == Visibility.Visible){
                _studyDataClient.CreateClaim(_loginName, 6, tbZipCode.Text);
            }
            if (pnlCountry.Visibility == Visibility.Visible)
            {
                _studyDataClient.CreateClaim(_loginName, 7, tbCountry.Text);
            }
            
        }
        
        public enum ClaimStatus { Completed, Cancelled };
        public delegate void ClaimsCompletedDelegate(ClaimStatus Status);
        public event ClaimsCompletedDelegate ClaimsCompleted;
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            SaveAllValues();
            ClaimsCompleted(ClaimStatus.Completed);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ClaimsCompleted(ClaimStatus.Cancelled);
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            PerformValidation();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            PerformValidation();
        }

        private void calBirthDate_DisplayDateChanged(object sender, SelectionChangedEventArgs e)
        {
            PerformValidation();
        }
    }
}
